package com.cloudree.gllib.render.texture;

import android.opengl.GLES20;
import android.opengl.Matrix;
import android.os.SystemClock;

import com.cloudree.gllib.BaseApp;
import com.cloudree.gllib.R;
import com.cloudree.gllib.object.Cube;
import com.cloudree.gllib.program.CubeProgram;
import com.cloudree.gllib.render.BaseRender;
import com.cloudree.gllib.util.GLHelper;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by Administrator on 2017/10/17 0017.
 */
public class MultiTextureRender extends BaseRender {

    private Cube mCube;
    private CubeProgram mProgram;
    private int[] textures;
    private int index;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Set the background clear color to black.
//        GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        // Use culling to remove back faces.
        GLES20.glEnable(GLES20.GL_CULL_FACE);
        // Enable depth testing
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);

        // Position the eye in front of the origin.
        final float eyeX = 0.0f;
        final float eyeY = 0.0f;
        final float eyeZ = -0.5f;

        // We are looking toward the distance
        final float lookX = 0.0f;
        final float lookY = 0.0f;
        final float lookZ = -5.0f;

        // Set our up vector. This is where our head would be pointing were we holding the camera.
        final float upX = 0.0f;
        final float upY = 1.0f;
        final float upZ = 0.0f;

        // Set the view matrix. This matrix can be said to represent the camera position.
        // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
        // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
        Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);

        mCube = new Cube();
        mProgram = new CubeProgram(BaseApp.getApp(), R.raw.obj_cube_vertex, R.raw.obj_cube_fragment);
        int[] resIds = {R.mipmap.bricks, R.mipmap.grass, R.mipmap.table, R.mipmap.ic_launcher, R.mipmap.avator, R.mipmap.tool_kit};
        textures = GLHelper.loadTextures(BaseApp.getApp(), resIds);

        index = 0;
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // Set the OpenGL viewport to the same size as the surface.
        GLES20.glViewport(0, 0, width, height);

        // Create a new perspective projection matrix. The height will stay the same
        // while the width will vary as per aspect ratio.
        final float ratio = (float) width / height;
        final float left = -ratio;
        final float right = ratio;
        final float bottom = -1.0f;
        final float top = 1.0f;
        final float near = 1.0f;
        final float far = 10.0f;

        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

        setLocation();
        mProgram.useProgram();
        mCube.bindData(mProgram);
        mProgram.setUniforms(mMVPMatrix, 1.0f, 0f, 0f, textures[0]);
        mCube.draw(0,6);
        mProgram.setUniforms(mMVPMatrix, 1.0f, 0f, 0f, textures[1]);
        mCube.draw(6,6);
        mProgram.setUniforms(mMVPMatrix, 1.0f, 0f, 0f, textures[2]);
        mCube.draw(12,6);
        mProgram.setUniforms(mMVPMatrix, 1.0f, 0f, 0f, textures[3]);
        mCube.draw(18,6);
        mProgram.setUniforms(mMVPMatrix, 1.0f, 0f, 0f, textures[4]);
        mCube.draw(24,6);
        mProgram.setUniforms(mMVPMatrix, 1.0f, 0f, 0f, textures[5]);
        mCube.draw(30,6);


    }

    /**
     * set location
     */
    private void setLocation() {
        // Do a complete rotation every 10 seconds.
        long time = SystemClock.uptimeMillis() % 10000L;
        float angleInDegrees = (360.0f / 10000.0f) * ((int) time);

        if (angleInDegrees % 360.0f < 1.0) {
            index++;
            if (index >= textures.length) index = 0;
        }

        // Draw some cubes.
        Matrix.setIdentityM(mModelMatrix, 0);
        Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -7.0f);
        Matrix.rotateM(mModelMatrix, 0, 30, 0.0f, 1.0f, 0.0f);
        Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 0.0f, 0.0f);
        Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mModelMatrix, 0);
    }
}
